package com.syni.mdd.yhd.common.all.service.impl.groupBuy;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.groupBuy.BmsUserBargainDao;
import com.syni.mdd.yhd.common.all.dao.jpa.groupBuy.BmsUserHelpBargainDao;
import com.syni.mdd.yhd.common.all.dto.reponseDto.groupBuy.bargain.BargainActionRpDto;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.groupBuy.BmsUserBargain;
import com.syni.mdd.yhd.common.all.entity.groupBuy.BmsUserHelpBargain;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.service.BmsUserService;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsUserBargainService;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsUserHelpBargainService;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsUserHelpBargainServiceImpl extends BaseService<BmsUserHelpBargain,Integer> implements BmsUserHelpBargainService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsUserHelpBargainDao bmsUserHelpBargainDao;

    @Autowired
    BmsUserService bmsUserService;

    @Override
    public BmsUserHelpBargainDao getBaseDao() {
        return bmsUserHelpBargainDao;
    }

    @Override
    public Page<BmsUserHelpBargain> getOtherHelpRecord(Integer bargainId, Integer userId,
                                                       Integer pageNum, Integer pageSize) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("userBargainId",bargainId);
        conditionMap.put("userId_NEQ",userId);
        Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize, Sort.Direction.DESC,"newTime");
        return bmsUserHelpBargainDao.findAll(new SpecificationUtils(conditionMap),pageable);
    }

    @Override
    public Page<BmsUserHelpBargain> getHelpRecord(Integer bargainId,Integer pageNum, Integer pageSize) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("userBargainId",bargainId);
        Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize, Sort.Direction.DESC,"newTime");
        return bmsUserHelpBargainDao.findAll(new SpecificationUtils(conditionMap),pageable);
    }

    @Override
    public void addBargainRecord(Integer userId, Integer bargainId,Double cutPrice) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("userId",userId);
        conditionMap.put("userBargainId",bargainId);
        conditionMap.put("newTime",new Date());
        List<BmsUserHelpBargain> bmsUserHelpBargains = bmsUserHelpBargainDao.findAll(new SpecificationUtils(conditionMap));
        if(!(bmsUserHelpBargains != null && bmsUserHelpBargains.size() > 0)){
            BmsUserHelpBargain bmsUserHelpBargain = new BmsUserHelpBargain();
            bmsUserHelpBargain.setUserId(userId);
            bmsUserHelpBargain.setUserBargainId(bargainId);
            bmsUserHelpBargain.setNewTime(new Date());
            BmsUser bmsUser = TokenManager.getUser(userId);
            if(bmsUser != null) {
                bmsUserHelpBargain.setLoginName(BmsUserService.getWechatLoginName(bmsUser));
                bmsUserHelpBargain.setHeadImg(BmsUserService.getWechatHeadOrAppHeadImg(bmsUser));
            }
            bmsUserHelpBargain.setUserBargainId(bargainId);
            bmsUserHelpBargain.setCutPrice(cutPrice);
            bmsUserHelpBargainDao.save(bmsUserHelpBargain);
        }
    }

    @Override
    public List<BmsUserHelpBargain> findUserHelpRecord(Integer userId) {
        Pageable pageable = BmsCommonService.getPageable(1,1);
        return bmsUserHelpBargainDao.findByUserId(userId,pageable);
    }

    @Override
    public List<BmsUserHelpBargain> getOtherHelpRecord(Integer bargainId, Integer userId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("userBargainId",bargainId);
        conditionMap.put("userId_NEQ",userId);
        return bmsUserHelpBargainDao.findAll(new SpecificationUtils(conditionMap));
    }

    @Override
    public List<BmsUserHelpBargain> getHelpRecord(Integer bargainId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("userBargainId",bargainId);
        return bmsUserHelpBargainDao.findAll(new SpecificationUtils(conditionMap));
    }
}
